package org.nuaa.client;
/* 界面 */

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class ClientInter extends JFrame implements ActionListener {
    JTextArea jta;
    JTextField jtf_times;
    JButton jb_down, jb_up,jb_accept,jb_reject;
    JPanel jp;
    Client client;
    Integer accept;
    Socket socket;

    public ClientInter() throws IOException { //放置各种部件和按钮
        setTitle("客户端");
        accept = new Integer(0);
        jta = new JTextArea();//显示下载信息
        jtf_times = new JTextField("片数", 3);//输入分片数栏,即发送tcp报文的数量
        jb_down = new JButton("确认下载");
        jb_down.addActionListener(this);//对jb_down增加监听，进行某一个操作的时候触发某项功能
        jb_up = new JButton("确认上传");
        jb_accept = new JButton("确认接受");
        jb_accept.addActionListener(this);

        jb_reject = new JButton("确认拒绝");
        jb_reject.addActionListener(this);
        jb_up.addActionListener(this);
        jp = new JPanel();
        jp.add(jtf_times);
        jp.add(jb_down);
        jp.add(jb_up);
        jp.add(jb_accept);
        jp.add(jb_reject);
        this.add(jta, "Center");
        this.add(jp, "South");
        this.setSize(500, 300);
        this.setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//调用任意已注册 WindowListener 的对象后自动隐藏并释放该窗体
        firstConnect();

        client = new Client(jta,socket);

        /**窗体关闭按钮事件*/
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                if (JOptionPane.showConfirmDialog(null, "<html>确定退出吗？", "系统提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE) == 0) {
                    System.exit(0);
                }

                return;

            }
        });
    }

    private void firstConnect() throws IOException {
        String ip = JOptionPane.showInputDialog("请输入服务器IP：");
        this.socket = new Socket(ip, 9999);
        InputStream is = socket.getInputStream();//有问题
        byte[] buf1 = new byte[1024];
        int len1 = is.read(buf1);
        String str2 = new String(buf1, 0, len1);
        System.out.println("准备读信息");
        System.out.println(str2);
        jta.append("服务器即将发送如下文件：\n" + str2 + "\n是否接受？");//显示文件信息


    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        // 假如点击了按钮jb_down
        if (arg0.getSource() == jb_down) {

            //getText返回数据窗口控件中悬浮在当前行列之上的编辑框中的文本
            //trim() 方法用于删除字符串的头尾空白符
            //toCharArray() 方法将字符串转换为字符数组
            char[] timesnum = jtf_times.getText().trim().toCharArray();
            double times_num = timesnum[0] - '0';
            System.out.println(times_num);

            //				Socket socket = new Socket("127.0.0.1",9999);
//				InputStream is = socket.getInputStream();
//				byte[] buf = new byte[1024];
//				System.out.println("here");
//				int len = is.read(buf);
//				String str2 = new String(buf,0,len);
//
//				System.out.println(str2);
//				DataOutputStream dos =  new DataOutputStream(socket.getOutputStream());
//				dos.writeInt(1);
//				dos.write(times_num);
//				dos.write(choice);
//				dos.close();
//				is.close();//关闭读入流(从客户端传来的信息）
//				socket.close();
            client.Download(0, times_num,accept);
        }
        if(arg0.getSource()==jb_accept){
            accept=1;
            jta.append("已确定" + (accept == 1 ? "" : "不") + "接受");
        }
        if(arg0.getSource()==jb_reject){
            accept=0;
            jta.append("已确定" + (accept == 1 ? "" : "不") + "接受");
        }
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        new ClientInter();
    }

}
